package thread;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentManage extends JFrame implements ActionListener {
    private JTextField[] tf;
    private JButton addBtn;
    private JTable table;
    private DefaultTableModel model;
    private List<Student> stus;

    public StudentManage() {
        setTitle("学生管理");
        setSize(800, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);
        String[] str = {"姓名", "年龄", "地址"};
        tf = new JTextField[3];
        int y = 50;
        for (int i = 0; i < str.length; i++, y = y + 50) {
            JLabel label = new JLabel(str[i]);
            label.setBounds(50, y, 60, 25);
            add(label);
            tf[i] = new JTextField();
            tf[i].setBounds(110, y, 100, 25);
            add(tf[i]);
        }
        addBtn = new JButton("添加");
        addBtn.setBounds(100, y, 60, 25);
        addBtn.addActionListener(this);
        add(addBtn);
        model = new DefaultTableModel();
        model.setDataVector(new Object[][]{}, str);
        table = new JTable(model);
        table.setBounds(350, 50, 400, 25);
        JScrollPane sp = new JScrollPane(table);
        sp.setBounds(350, 50, 400, 300);
        add(sp);
//        stus = new ArrayList<>();
//        stus.add(new Student("孙悟空", 23, "昆明"));
//        stus.add(new Student("唐僧", 20, "上海"));
//        stus.add(new Student("猪八戒", 23, "北京"));
        try {
            stus=readFile("d:\\student.dat");
            loadDate(stus);
        } catch (Exception e) {
           // e.printStackTrace();
        }

    }

    public void writeFile(List<? extends Student> stus) throws IOException {
        File file = new File("d:\\student.dat");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(stus);
        oos.close();
    }

    public List<Student> readFile(String path) throws IOException,ClassNotFoundException{
        File file=new File(path);
        List<Student> stus;
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
        stus=(List<Student>) ois.readObject();
        ois.close();
        return stus;
    }


    public  void loadDate(List<? extends Student>stus){
        int c=model.getRowCount();
        for (int i=0;i<c;i++){
            model.removeRow(0);
        }
        for (Student s:stus){
            model.addRow(s.toArry());

        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==addBtn);
        Student s=new Student();
        s.setName(tf[0].getText());
        s.setAge(Integer.valueOf(tf[1].getText()));
        s.setAddr(tf[2].getText());
        if (stus==null) stus=new ArrayList<>();
        stus.add(s);
        try {
            writeFile(stus);
            loadDate(stus);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StudentManage();

            }
        });
    }
}
